package cn.enine.tbcharge.ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cn.enine.tbcharge.R;
import cn.enine.tbcharge.base.MyBaseActivity;
import cn.enine.tbcharge.pojo.AppUser;
import cn.enine.tbcharge.pojo.RetMsg;
import cn.enine.tbcharge.util.HttpUrl;
import cn.enine.tbcharge.util.MyToastUtil;
import cn.enine.tbcharge.util.NetWorkUtils;
import cn.enine.tbcharge.util.RegexUtils;
import cn.enine.tbcharge.util.SharedPreferencesUtils;
import cn.enine.tbcharge.util.StringUtils;
import cn.enine.tbcharge.util.UserClient;
import cn.pedant.SweetAlert.SweetAlertDialog;

public class ResetPasswordActivity extends MyBaseActivity {

    //标题
    TextView title, uname;
    LinearLayout back;
    Context con = ResetPasswordActivity.this;

    private EditText oldmm, newmm, newmmm;
    AppUser obj;
    String url="";

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_update_password);
        if (NetWorkUtils.isNetworkConnected(con)) {
            init(obj);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    private void init(final AppUser obj){
        oldmm = (EditText) findViewById(R.id.oldmm);
        newmm = (EditText) findViewById(R.id.newmm);
        newmmm = (EditText) findViewById(R.id.newmmm);
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("修改密码");
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String oldmmText = oldmm.getText().toString();
                final String newmmText = newmm.getText().toString();
                String newmmmText = newmmm.getText().toString();

                if(null == oldmmText || "".equals(oldmmText) ){
                    MyToastUtil.ShowToast(con,"请输入旧密码");
                }else if(null == newmmText || "".equals(newmmText) ){
                    MyToastUtil.ShowToast(con,"请输入新密码");
                }else if(null == newmmmText || "".equals(newmmmText) ){
                    MyToastUtil.ShowToast(con,"请再输一遍新密码");
                }else if(!newmmText.equals(newmmmText) ){
                    MyToastUtil.ShowToast(con,"输入两遍新密码不一致");
                }else if (NetWorkUtils.isNetworkConnected(con)) {  ///网络连接
                    url = HttpUrl.EditPass;
                    RequestParams ps = new RequestParams();
                    ps.add("oldmm", oldmmText);
                    ps.add("newmm", newmmText);
                    ps.add("newmmm", newmmmText);
                    ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                    ps.add("type", "2");
                    UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(String content) {
                            super.onSuccess(content);
                            RetMsg ret = JSON.parseObject(content, RetMsg.class);
                            if (ret.getCode().equals("202")) {
                                MyToastUtil.ShowToast(con, "旧密码输入错误");
                            }else if (ret.getCode().equals("200")){
                                MyToastUtil.ShowToast(con, "成功");
                                SharedPreferencesUtils.setParam(con, "password", newmmText.toString());
                                finish();
                            }else{
                                MyToastUtil.ShowToast(con, "失败");
                            }
                        }
                        @Override
                        public void onFailure(Throwable error) {
                            super.onFailure(error);
                            MyToastUtil.ShowToast(con, "失败");
                        }
                    });
                } else {

                }
            }
        });

    }
}
